package com.chenyue.cm.utils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 导入导出公共使用方法
 * fff 2020-10-30
 */
public  class ExportUtill {
    /**
     * 导入导出标识（内部使用 0-学校信息  1-教授信息 2-课程信息）
     */
    public static final String SCHOOL = "0";
    public static final String PROFESSOR = "1";
    public static final String CLASS = "2";
    public static Map getExportMap(String type) {
        LinkedHashMap resultMap =new LinkedHashMap();
        if(type.equals(ExportUtill.SCHOOL)){
            resultMap.put("schoolName", "学校名称");
            resultMap.put("pic", "图片");
            resultMap.put("address", "地址");
            resultMap.put("createTime", "信息录入时间");
        }else if (type.equals(ExportUtill.PROFESSOR)){
            resultMap.put("schoolName", "学校名称");
            resultMap.put("professorName", "教授名称");
            resultMap.put("ct", "信息录入时间");
        } else {
            resultMap.put("schoolName", "学校名称");
            resultMap.put("professorName", "教授名称");
            resultMap.put("className", "课程名称");
            resultMap.put("ct", "信息录入时间");
        }
        return resultMap;
    }

    //request请求数据转换成map
    public static Map getParameterMap(HttpServletRequest request) {
        // 参数Map
        Map properties = request.getParameterMap();
        // 返回值Map
        Map returnMap = new HashMap();
        Iterator entries = properties.entrySet().iterator();
        Map.Entry entry;
        String name = "";
        String value = "";
        while (entries.hasNext()) {
            entry = (Map.Entry) entries.next();
            name = (String) entry.getKey();
            Object valueObj = entry.getValue();
            if(null == valueObj){
                value = "";
            }else if(valueObj instanceof String[]){
                String[] values = (String[])valueObj;
                for(int i=0;i<values.length;i++){
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length()-1);
            }else{
                value = valueObj.toString();
            }
            returnMap.put(name, value);
        }
        return returnMap;
    }
}